﻿package com.codingbros.particles 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import flash.events.Event;
	
	import flash.geom.ColorTransform;
	
	
	public class ParticleFlowSystem extends ParticleSystem
	{		
		private var noiseOffset:Array = new Array(new Point(), new Point());
		private var noiseOffsetAdd:Number = 0.5;
		
		private var particleNoiseBitmap:Bitmap;
		private var particleNoiseBitmapData:BitmapData;
		
		public function ParticleFlowSystem(width:int, height:int) 
		{
			super(width, height);
			
			numberOfParticles = 20;
			
			var p:Particle;
			
			for (var i:int = 0; i < particlesNum; i ++) 
			{
				p = particles[i];
				p.x = systemWidth - 1;
				p.y = i / particlesNum * systemHeight;
			}
			
			particleNoiseBitmapData = new BitmapData(systemWidth, systemHeight);
			
			particleNoiseBitmap = new Bitmap(particleNoiseBitmapData);
			particleNoiseBitmap.visible = false;
			this.addChild(particleNoiseBitmap);			
		}
		
		override protected function updateSystem(e:Event):void 
		{
			noiseOffset[0].x += noiseOffsetAdd;
			noiseOffset[0].y += noiseOffsetAdd;
			
			noiseOffset[1].x -= noiseOffsetAdd;
			noiseOffset[1].y -= noiseOffsetAdd;
			
			particleNoiseBitmapData.perlinNoise(50, 50, 1, 100, false, true, 7, true, noiseOffset);
			
			particlesBitmapData.colorTransform(particlesBitmapData.rect, new ColorTransform( 1, 1, 1, 1, 0, 0, 0, -1 ) );
			
			particlesCanvas.graphics.clear();
			particlesCanvas.graphics.lineStyle(1, 0x000000);
			
			var p:Particle;
			
			var brightness:Number;
			var normRadians:Number = Math.PI * 1.25;
			var diffRadians:Number;
			var noiseRadians:Number;
			var radiansRation:Number = 0;
			
			for (var i:int = 0; i < particlesNum; i ++) 
			{
				p = particles[i];
				
				brightness = particleNoiseBitmapData.getPixel(p.x, p.y) / 0xFFFFFF;
				
				noiseRadians = Math.PI * 0.75 +  (Math.PI) * brightness;
				diffRadians = normRadians - noiseRadians;
				
				radiansRation = 1.0 - (p.x / Number(systemWidth));
				
				/*
				if (i == 2)
				{
					trace("x = " + p.x);
					trace("radiansRation = " + radiansRation);
					trace("normRadians = " + normRadians);
					trace("noiseRadians = " + noiseRadians);
					trace("diffRadians = " + diffRadians);
					trace();
				}
				*/
				
				p.vx = Math.cos( normRadians +  diffRadians *  radiansRation) * PARTICLEMAXSPEED;
				p.vy = Math.sin( normRadians +  diffRadians *  radiansRation ) * PARTICLEMAXSPEED;
				
				particlesCanvas.graphics.moveTo(p.x, p.y);
				
				p.x += p.vx;
				p.y += p.vy;
					
				particlesCanvas.graphics.lineTo(p.x, p.y);
				
				if (p.x <= 0 || p.x > systemWidth || p.y < 0 || p.y > systemHeight) {
					p.x = systemWidth - 1;
					p.y = i / particlesNum * systemHeight;
				}
			}
			
			particlesBitmapData.draw(particlesCanvas);
			
		}
		
	}
	
}